package com.person.jc.service.security;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.CreateCode;
import com.person.jc.utils.Utils;

public class TsDeptService extends Service {
	
	public static final TsDeptService me = new TsDeptService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT t.*,IFNULL(t1.dept_name,'无') pdept_name FROM ts_dept t ");
		sql.append(" LEFT JOIN ts_dept t1 ON t.p_dept_id = t1.dept_id ");
		sql.append(" WHERE t.status <> 3");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("pdeptId")))){
			sql.append(" and t.p_dept_id = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deptId")))){
			sql.append(" and t.dept_id = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deptName")))){
			sql.append(" and t.dept_name like ? ");
			params.add("%"+temp+"%");
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("status")))){
			sql.append(" and t.status = ? ");
			params.add(temp);
		}
		return Db.find(sql.toString(), Utils.getObjParam(params));
	}
	
	public void save(Map<String,Object> map){
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append(" insert into ts_dept(dept_id,p_dept_id,dept_name,status,idx_no) ");
		sql.append(" values (?,?,?,?,?)");
		String pdeptId = String.valueOf(map.get("pDeptId"));
		params.add(CreateCode.createDeptId(pdeptId));
		params.add(pdeptId);
		params.add(map.get("deptName"));
		params.add(map.get("status"));
		params.add(map.get("idxNo"));
		Db.update(sql.toString(), Utils.getObjParam(params));
		super.setCRUD("部门管理", "C", "新增部门:" + map.get("deptName"));
	}
	
	public void update(Map<String,Object> map){
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append(" update ts_dept set");
		sql.append(" dept_name = ? ,");
		sql.append(" status = ? , ");
		sql.append(" idx_no = ? ");
		sql.append(" where dept_id = ? ");
		params.add(map.get("deptName"));
		params.add(map.get("status"));
		params.add(map.get("idxNo"));
		params.add(map.get("deptId"));
		Db.update(sql.toString(), Utils.getObjParam(params));
		super.setCRUD("部门管理", "U", "修改部门:"+map.get("deptId"));
	}
	
	public void updateStatus(String ids,String status){
		String[] id = ids.split(",");
		Object[][] paras = new Object[id.length][2];
		for (int i = 0; i < id.length; i++) {
			paras[i][0] = status;
			paras[i][1] = id[i];
		}
		String sql = " update ts_dept set status = ? where dept_id = ?";
		Db.batch(sql, paras, id.length);
		super.setCRUD("角色管理", "U", "更改部门状态:"+ids+"("+status+")");
	}
	
	public int checkName(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("select count(1) from ts_dept where 1=1 and status <> '3'");
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deptName")))){
			sql.append(" and dept_name = ? ");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("deptId")))){
			sql.append(" and dept_id <> ?");
			params.add(temp);
		}
		return Integer.parseInt(String.valueOf(Db.queryLong(sql.toString(), Utils.getObjParam(params))));
	}
	
	public String getDeptNameById(String deptId) {
        String sql = "select dept_name from ts_dept where dept_id = ? ";
        return Db.queryStr(sql, new Object[] { deptId });
    }
}
